package com.lagou.config;

import com.lagou.callbackconfig.MsgSendConfirmCallback;
import com.lagou.callbackconfig.MsgSendReturnCallback;
import com.lagou.constant.RabbitMQConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * @Description:
 * @Author: qjc
 * @Date: 2021/12/9
 */
@Configuration
@Slf4j
public class RabbitMqConfig {

    @Resource
    private QueueConfig queueConfig;
    @Resource
    private ExchangeConfig exchangeConfig;
    @Resource
    private ConnectionFactory connectionFactory;


    @Bean
    public Binding bindingDirectDLXNormal() {
        return BindingBuilder.bind(queueConfig.dlxNormalQueue()).to(exchangeConfig.dlxNormalExchange()).with(RabbitMQConstant.ROUTING_KEY_DLX_NORMAL);
    }

    @Bean
    public Binding bindingDirectDLX() {
        return BindingBuilder.bind(queueConfig.dlxQueue()).to(exchangeConfig.dlxExchange()).with(RabbitMQConstant.ROUTING_KEY_DLX);
    }

    @Bean
    public RabbitTemplate rabbitTemplate() {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMandatory(true);
        rabbitTemplate.setConfirmCallback(new MsgSendConfirmCallback());
        rabbitTemplate.setReturnCallback(new MsgSendReturnCallback());
        return rabbitTemplate;
    }

}
